<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="关键词">
        <el-input v-model="queryParams.keyword" placeholder="" />
      </el-form-item>
      <el-form-item label="开始时间">
        <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="queryParams.startTime" type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间">
        <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model="queryParams.endTime" type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:role:add']"
              >新增</el-button
            >
          </el-col> -->
      <el-col :span="1.5">
        <!-- <el-button
                  type="success"
                  plain
                  icon="el-icon-edit"
                  size="mini"
                  :disabled="single"
                  @click="handleUpdate"
                  v-hasPermi="['system:role:edit']"
                  >修改</el-button
                > -->
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['system:role:remove']"
                >删除</el-button
              > -->
      </el-col>
      <el-col v-if="user == 'admin'" :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:role:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table @sort-change="paixu" v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="客服" prop="orderService" />

      <el-table-column label="总应收" sortable="custom" prop="transactionPrice">
      </el-table-column>

      <el-table-column label="成单量" sortable="custom" prop="orderCount" />
      <el-table-column label="老师报酬" sortable="custom" prop="teacherTotalPrice" />

      <el-table-column fixed="right" label="渠道收入情况" align="center" width="550" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-table :data="scope.row.voList">
            <el-table-column label="渠道" prop="attribute" />

            <el-table-column label="应收" prop="billReceivable">
            </el-table-column>


          </el-table>
        </template>
      </el-table-column>
    </el-table>

    <tab :zhengroleList="zhengroleList" />
    <pagination :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import {
  listRole,
  getRole,
  delRole,
  addRole,
  updateRole,
  dataScope,
  changeRoleStatus,
  deptTreeSelect,
} from "@/api/system/role";
import { deleteixundan, paizneg, daochuxiaoshou } from "@/api/new";

import { listUser, duizhangon, keudi, xiaoshouyeji } from "../../api/new";
import tab from '../statistics/tab.vue'

import {
  treeselect as menuTreeselect,
  roleMenuTreeselect,
} from "@/api/system/menu";

export default {
  name: "Role",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      ketui: "",
      jiaojie: "",
      dai: "",
      // 遮罩层
      loading: true,
      // 选中数组
      xunkong: false,
      ids: [],
      xunkong: false,
      // 非单个禁用
      single: true,
      now: "",
      hounow: 0,
      // 非多个禁用
      kesuwanfrom: {
        recordId: "",
        workDate: "",
        workOne: "",
        workTwo: "",
        restWorker: "",
        remake: null,
      },
      kesuwanfromre: {
        workDate: [
          { required: true, message: "日期不能为空", trigger: "blur" },
        ],
        workOne: [{ required: true, message: "员工不能为空", trigger: "blur" }],
        workTwo: [{ required: true, message: "员工不能为空", trigger: "blur" }],
        restWorker: [
          { required: true, message: "员工不能为空", trigger: "blur" },
        ],
      },
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      user: this.$store.state.user.name,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 数据范围选项

      yiwei: [
        {
          value: "已成单",
          label: "已成单",
        },
        {
          value: "未成单",
          label: "未成单",
        },
      ],
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        sortField: null,
        orderingRule: null,
        pageNo: 1,
        pageSize: 10,
        startTime: null,
        endTime: null,
        keyword: null,
      },
      chengdan: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 表单校验
      rules: {
        roleName: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
        roleKey: [
          { required: true, message: "权限字符不能为空", trigger: "blur" },
        ],
        roleSort: [
          { required: true, message: "角色顺序不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },

  methods: {
    xundan() {
      this.$refs["xundanfrom"].validate((valid) => {
        if (valid) {
          if (this.title !== "添加") {
            paizneg([this.kesuwanfrom]).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.xunkong = false;
              this.getList();
            });
          } else {
            paizneg([this.kesuwanfrom]).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.xunkong = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      xiaoshouyeji(this.queryParams).then((response) => {
        this.roleList = response.data.servicePerformanceVos;

        console.log(this.roleList);
        // this.total = response.total;
        this.loading = false;
      });
    },
    paixu(e, d) {
      let xu = "";
      if (e.order == "descending") xu = "desc";
      if (e.order == "ascending") xu = "asc";
      // let a = e.prop.replace(/([A-Z])/g, "_$1").toLowerCase();

      this.queryParams.sortField = e.prop;
      this.queryParams.orderingRule = xu;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      (this.menuExpand = false),
        (this.menuNodeAll = false),
        (this.deptExpand = true),
        (this.deptNodeAll = false),
        (this.form = {
          complaintId: undefined,
          roleName: undefined,
          roleKey: undefined,
          roleSort: 0,
          status: "0",
          menuIds: [],
          deptIds: [],
          menuCheckStrictly: true,
          deptCheckStrictly: true,
          remark: undefined,
        });
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.startTime = "";
      this.queryParams.endTime = "";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.followId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleDataScope":
          this.handleDataScope(row);
          break;
        case "handleAuthUser":
          this.handleAuthUser(row);
          break;
        default:
          break;
      }
    },
    // 树权限（展开/折叠）

    guanbishoukan() {
      this.kesuwanfrom = {
        recordId: "",
        workDate: this.timestampToDateString(+new Date()),
        workOne: "",
        workTwo: "",
        restWorker: "",
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.kesuwanfrom.workDate = this.timestampToDateString(+new Date());
      this.xunkong = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      keudi({ recordId: row.recordId, checkStatus: "无误" }).then(() => {
        this.$modal.msgSuccess("核对成功");
        this.getList();
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.complaintId != undefined) {
            this.form.menuIds = this.getMenuAllCheckedKeys();
            updateRole(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.menuIds = this.getMenuAllCheckedKeys();
            addRole(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮（数据权限） */
    submitDataScope: function () {
      if (this.form.complaintId != undefined) {
        this.form.deptIds = this.getDeptAllCheckedKeys();
        dataScope(this.form).then((response) => {
          this.$modal.msgSuccess("修改成功");
          this.openDataScope = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roleIds = row.followId || this.ids;
      this.$modal
        .confirm("是否确认删除数据项？")
        .then(function () {
          console.log(roleIds);
          return deleteixundan(roleIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/system/orderForm/servicePerformanceExportNew",
        {
          ...this.queryParams,
        },
        `销售业绩${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>